//
//  LPVideoViewTableGGBPlayListViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPVideoViewTableGGBPlayListViewController.h"
#import "LPZWYChooseClassClassModel.h"
#import "LPVideoTableDetailTableViewCell.h"
#define classCell @"LPVideoViewTableGGBPlayListViewController"


@interface LPVideoViewTableGGBPlayListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _seleIndex;/**< 选中的cell  默认0 */
}
/**    */
@property (strong, nonatomic) UITableView  *tableView;
/**    */
@property (strong, nonatomic) NSMutableArray   *dataArray;

@end

@implementation LPVideoViewTableGGBPlayListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _seleIndex = 0;
    self.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:self.model.dataSouce] mutableCopy];
    [self layoutUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPlayerVideoIndex:) name:Notification_Receive_VideoID object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPlayerVideoIndex_One:) name:Notification_Receive_VideoID_ONE object:nil];
    // Do any additional setup after loading the view.
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 私有方法


#pragma mark - 监听事件
- (void)getPlayerVideoIndex:(NSNotification *)Info{
    NSDictionary *dict = Info.object;
    LPZWYChooseClassClassModel *model = [dict objectForKey:@"Model"];
    __block NSInteger index = 0;
    [self.dataArray enumerateObjectsUsingBlock:^(LPZWYChooseClassClassModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.ID isEqualToString:model.ID]) {
            index = idx;
        }
    }];
    [self changeCellMarkRedFromOldIndex:_seleIndex newIndex:index isPlayerOver:YES];
}

- (void)getPlayerVideoIndex_One:(NSNotification *)Info{
    NSDictionary *dict = Info.object;
    LPZWYChooseClassClassModel *model = [dict objectForKey:@"Model"];
    __block NSInteger index = 0;
    [self.dataArray enumerateObjectsUsingBlock:^(LPZWYChooseClassClassModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.ID isEqualToString:model.ID]) {
            index = idx;
        }
    }];
    [self changeCellMarkRedFromOldIndex:_seleIndex newIndex:index isPlayerOver:NO];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPVideoTableDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
    cell.isShowMySelf = YES;
    if (_seleIndex == indexPath.row) {
        cell.isTitleRed = YES;
    }else{
        cell.isTitleRed = NO;
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
    
}


/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != _seleIndex) {
        LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Push_VideoID object:@{@"videoID":model.ID}];
        [self changeCellMarkRedFromOldIndex:_seleIndex newIndex:indexPath.row isPlayerOver:NO];
    }
    
}



#pragma mark - 侧滑删除

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}


- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return NO;
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    if ([model.isrequire integerValue] == 3) {
        if (_seleIndex == indexPath.row) {
            return NO;
        }
        return YES;
    }
    return NO;
    
}


#pragma mark - cell标红的切换
- (void)changeCellMarkRedFromOldIndex:(NSInteger)oldIndex  newIndex:(NSInteger)newIndex isPlayerOver:(BOOL)isPlayerOver{
    LPVideoTableDetailTableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:oldIndex inSection:0]];
    //改变久cell的完成度
    LPZWYChooseClassClassModel *oldModel = self.dataArray[oldIndex];
    if (isPlayerOver) {
        [self getCourseInfo_model:oldModel index:oldIndex];
    }
    if (oldIndex != newIndex) {
        //隐藏标红
        oldCell.isTitleRed = NO;
        LPVideoTableDetailTableViewCell *newCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:newIndex inSection:0]];
        newCell.isTitleRed = YES;
        _seleIndex = newIndex;
    }
    
    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_seleIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


/**  实现了删除操作  */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    [YJProgressHUD showMessage:@"删除中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    NSString *coursewareid = model.coursewareid ? model.coursewareid:model.ID;
    [LPRequestManager request_deleteToBeStudiedUseWeicatAppWithcoursewareid:coursewareid successBlock:^(id object) {
        [YJProgressHUD hide];
        NSString *result  = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
           
            [weakSelf.tableView reloadData];
            ///修改了数据源，对课程详情赋值
            [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Push_VideoID object:@{@"Array":self.dataArray}];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
    }];
}



///课程基本信息
- (void)getCourseInfo_model:(LPZWYChooseClassClassModel *)model index:(NSInteger)index{
    __weak typeof(self) weakSelf = self;
    [ZJRequestManager request_getCoursewareInfoWithID:model.ID successBlock:^(id object) {
        if ([[object objectForKey:@"code"] integerValue] == 1) {
            NSArray *array = [object objectForKey:@"data"];
            if (array.count > 0) {
                LPZWYChooseClassClassModel * newModel = [LPZWYChooseClassClassModel yy_modelWithJSON:array.firstObject];
                model.playpercentage = newModel.playpercentage;
                weakSelf.dataArray[index] = model;
                [weakSelf.tableView reloadData];
            }
        }
    } failBlock:^(id object) {
    }];
}



#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [UIScreen zj_screenHeight] - LP_NavViewHeight - 100 * [UIScreen zj_screenHScale] - [UIScreen zj_screenWith] / 16 * 9 - 47 * [UIScreen zj_screenHScale]- [NSString heightWithTextFont:LYPFONT(18)] - 1 ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LPVideoTableDetailTableViewCell class] forCellReuseIdentifier:classCell];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}


@end
